package net.shop.action.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.shop.biz.db.ProductVirtualCategoryService;
import net.shop.biz.service.FriendlyUrlService;
import net.shop.common.SystemConstants;
import net.shop.dal.entity.ProductVirtualCategory;

import org.apache.commons.lang.StringUtils;

import ths.web.action.ActionContext;
import ths.web.action.ActionException;
import ths.web.action.Result;
import ths.web.action.result.ResultView;
import ths.web.annotation.HttpAction;
import ths.web.servlet.BeanPropertyHttpParameterSource;
import ths.web.util.ParamUtils;

@HttpAction("admin")
public class ProductVirtualCategoryAction extends BaseAdminAction {
	@Resource
	private ProductVirtualCategoryService productVirtualCategoryService;
	@Resource
	private FriendlyUrlService friendlyUrlService;
	
	@Override
	public Result execute() throws ActionException {
		ActionContext context = ActionContext.getContext();	
		HttpServletRequest request = context.getRequest();
		
		String ac = ParamUtils.getTrimParameter(request, "ac");
		if (StringUtils.isEmpty(ac)) ac = "list";
		
		if (StringUtils.equals("list", ac)) {
			return list(context);
		} else if (StringUtils.equals("add", ac)) {
			return add(context);
		} else if (StringUtils.equals("add_save", ac)) {
			return addSave(context);
		}
		
		return null;
	}
	
	private Result list(ActionContext context) throws ActionException {
		ResultView view = getViewResult(context);
		managerBaseVo.setActionName(getActionName(context.getRequest()));
		view.assign("managerBase", managerBaseVo);
		
		return view.toView("product_virtual_category_list");		
	}
	
	private Result add(ActionContext context) throws ActionException {
		ResultView view = getViewResult(context);
		view.assign("managerBase", managerBaseVo);
		
		return view.toView("product_virtual_category_add");		
	}
	
	private Result addSave(ActionContext context) throws ActionException {
		HttpServletRequest request = context.getRequest();
		
		ProductVirtualCategory category = new ProductVirtualCategory();
		BeanPropertyHttpParameterSource source  = new BeanPropertyHttpParameterSource();
		source.setPropertys(category, request);

		short depth = 0;
		ProductVirtualCategory parent = null;
		if (category.getParentCid() > 0) {
			parent = productVirtualCategoryService.get(category.getParentCid());
			if (parent != null) {
				depth = parent.getDepth();
				depth++;
			}
		}
		
		category.setHaseCode(friendlyUrlService.getUrlHaseCode(category.getFriendlyUrl()));
		category.setIsParent(SystemConstants.BYTE_FALSE);
		category.setDepth(depth);		
		
		productVirtualCategoryService.save(category);
		if (parent != null && (SystemConstants.BYTE_FALSE == parent.getIsParent())) {
			parent.setIsParent(SystemConstants.BYTE_TRUE);
			productVirtualCategoryService.save(parent);
		}
		
		String url = getActionName(context.getRequest()) + "?ac=list";
		return this.getRedirectResult(context).jump(url);		
		
		/*
		private int categoryId;
		private long haseCode;
		private int parentCid;
		private boolean isParent;
		private short depth;
		private int reallyCid;
		private String categoryPic;
		private String brandIds;
		private String friendlyUrl;
		private String viewName;
		private short listOrder;
		private byte status;		
		*/
		/*
		short depth = 0;
		ProductVirtualCategory parent = null;
		if (parentId > 0) {
			parent = productVirtualCategoryService.get(parentId);
			if (parent != null) {
				depth = parent.getDepth();
				depth++;
			}
		}
		
		ProductVirtualCategory category = new ProductVirtualCategory();
		category.setHaseCode(friendlyUrlService.getUrlHaseCode(friendlyUrl));
		category.setParentCid(parentId);
		category.setIsParent(SystemConstants.BYTE_FALSE);
		category.setDepth(depth);
		category.setReallyCid(reallyCid);
		category.setCategoryPic(categoryPic);
		category.setFriendlyUrl(friendlyUrl);
		category.setViewName(viewName);
		category.setListOrder(sortOrder);
		
		productVirtualCategoryService.save(category);
		if (parent != null && (SystemConstants.BYTE_FALSE == parent.getIsParent())) {
			parent.setIsParent(SystemConstants.BYTE_TRUE);
			productVirtualCategoryService.save(parent);
		}
		
		String categoryName;
		List<Language> languages = getCurrentlySupportLanguages();
		for (Language l : languages) {
			categoryName = ParamUtils.getParameter(request, "view_name_"+ l.getLanguageId());
			System.out.println(categoryName);
		}	
		*/

	}
}
